package j_util.database;

import j_util.database.enums.DataTypeEnum;
import j_util.database.enums.ExceptionEnum;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public class SQL_Library {
    public static String buildCreateTableString(String tableTitle, String[] attributeName, String[] attributeType, long size[],
                                                String[] specification) {
        StringBuffer statement = new StringBuffer("create Table " + tableTitle);
        if (attributeType != null) {
            statement.append(" ( " + attributeName[0] + " " + attributeType[0]);
            if (size[0] != 0) {
                statement.append("(" + size[0] + ")");
            }
            if (specification[0] != null) {
                statement.append(" " + specification);
            }

            for (int j = 1; j < attributeName.length; j++) {
                statement.append(", ");
                statement.append(attributeName[j] + " " + attributeType[j]);
                if (size[j] != 0) {
                    statement.append("(" + size[j] + ")");
                }
                if (specification[j] != null) {
                    statement.append(" " + specification);
                }
            }
            statement.append(" )");
        }
        return statement.toString();
    }

    public static DataTypeEnum getDataType(String data) {
        try {
            Long.parseLong(data);
            return DataTypeEnum.LONG;
        } catch (NumberFormatException nfe) {
            try {
                Double.parseDouble(data);
                return DataTypeEnum.DOUBLE;
            } catch (NumberFormatException e) {
                if (data.equals("false") || data.equals("true")) {
                    return DataTypeEnum.BOOLEAN;
                } else {
                    return DataTypeEnum.STRING;
                }
            }
        }
    }

    /**
     * Identifies the SQLException and returns an ExceptionEnum containing the name of the object agent the exception.
     * This may be an existing table, a duplicate key.
     *
     * @param statement  - statement String;
     * @param stackTrace - stackTrace String;
     * @return exceptionEnum;
     */
    public static ExceptionEnum identifyException(String statement, String stackTrace) {
        statement = statement.replace("CREATE TABLE", "create Table");
        String[] arr = statement.split("create Table");
        if (arr.length == 2) {
            arr = arr[1].split(" ");
            ExceptionEnum exceptionEnum = ExceptionEnum.TABLE_ALREADY_CREATED;
            exceptionEnum.setInformation(arr[0]);
            return exceptionEnum;
        } else {
            arr = stackTrace.split("Violation of unique constraint");
            if (arr.length == 2) {
                arr = stackTrace.split("java.sql.SQLException: Violation of unique constraint SYS_PK_1314: duplicate value(s) for column(s) ");
                arr = arr[0].split(" ");
                ExceptionEnum exceptionEnum = ExceptionEnum.UNIQUE_CONSTRAINT_ALREADY_IN_USE;
                exceptionEnum.setInformation(arr[0]);
                return exceptionEnum;
            }
        }
        return ExceptionEnum.DEFAULT;
    }
}
